package com.victualler.core.config.permission.repository.impl;

import com.victualler.core.base.repository.impl.BaseQueryRepositoryImpl;
import com.victualler.core.config.permission.entity.RoleInfoDao;
import com.victualler.core.config.permission.entity.UserRoleDao;
import com.victualler.core.config.permission.repository.RoleInfoQueryRepository;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Guoshichang
 * @function 封装用于复杂sql查询的API. 为每个实体的repository层提供便利操作
 */
@Component
public class RoleInfoQueryRepositoryImpl extends BaseQueryRepositoryImpl<RoleInfoDao,Long> implements RoleInfoQueryRepository {
    /**
     * @author Guoshichang
     * @since 20:17 on 2018/7/5.
     * @param userId 用户id
     * @Function  获取用户id对应的角色列表
     */
    @Override
    public List<RoleInfoDao> getRoleInfoByUserId(Long userId) {
        //表连接查询
        String userRoleDaoTableName = this.getTableName(UserRoleDao.class);
        String roleInfoDaoTableName = this.getTableName(RoleInfoDao.class);
        String sql = "select * from "+roleInfoDaoTableName+" where id in (select role_Id from "+userRoleDaoTableName+" where  user_Id = ? )";
        return   this.queryListBySql(sql, new Object[]{userId});
    }

    /**
     * @author Guoshichang
     * @since 21:12 on 2018/9/9.
     * @param userId 双
     * @return List
     * @Function
     */
    public List<RoleInfoDao> notInByUserId(Long userId) {
        //表连接查询
        String userRoleDaoTableName = this.getTableName(UserRoleDao.class);
        String roleInfoDaoTableName = this.getTableName(RoleInfoDao.class);
        String sql = "select * from "+roleInfoDaoTableName+" where id not in (select role_Id from "+userRoleDaoTableName+" where  user_Id = ? )";
        return   this.queryListBySql(sql, new Object[]{userId});
    }
}
